// pages/search/search.js
let {
  getsearch,
  gethotsearch
} = require("../../api/search")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: '',
    keylists: [],
    mylists: null,
    one: true,
    two: false,
    three: false,
    discover_list: [],
  },
  find(e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  okfind() {
    wx.showLoading({
      title: '加载中',
    })
    getsearch({
      query: this.data.keyword
    }).then(data => {
      console.log(data);
      if (!data) {
        this.data.one = false
        this.data.two = true
        this.data.three = false
        this.setData(this.data)
        return
      } else {
        this.data.one = false
        this.data.two = false
        this.data.three = true
      }
      if (!data.data.list_v2.length) {
        this.data.one = false
        this.data.two = true
        this.data.three = false
        this.setData(this.data)
        return
      }
      if (data) {
        setTimeout(function () {
          wx.hideLoading()
        }, 2000)
      }
      let search = data.data.list_v2
      this.data.mylists = search
      if (this.data.keyword != '') {
        let gunLsts = this.data.keylists.includes(this.data.keyword)
        if(!gunLsts){
          this.data.keylists.push(this.data.keyword)
        }
        wx.setStorageSync('history', this.data.keylists)
      } else {
        this.data.one = false
        this.data.two = false
        this.data.three = true
      }
      this.setData(this.data)
    })
  },
  //搜索发现
  gun(e) {
    wx.showLoading({
      title: '加载中',
    })
    let index = e.target.dataset.index;
    console.log(index);
    console.log(e);
    console.log(this.data.discover_list[index].desc);
    getsearch({
      query: this.data.discover_list[index].desc
    }).then(data => {
      console.log(data);
      if (data) {
        setTimeout(function () {
          wx.hideLoading()
        }, 2000)
      }
      if (!data) {
        this.data.one = false
        this.data.two = true
        this.data.three = false
        this.setData(this.data)
      } else {
        this.data.one = false
        this.data.two = false
        this.data.three = true
        this.setData(this.data)
      }
      if (!data.data.list_v2.length) {
        this.data.one = false
        this.data.two = true
        this.data.three = false
        this.setData(this.data)
        return
      }

      let search = data.data.list_v2
      this.data.mylists = search
      if (this.data.discover_list[index].desc != '') {
        let gunLists = this.data.keylists.includes(this.data.discover_list[index].desc)
        console.log(gunLists);
        if (!gunLists) {
          this.data.keylists.push(this.data.discover_list[index].desc)
        }
        wx.setStorageSync('history', this.data.keylists)
      } else {
        this.data.one = false
        this.data.two = false
        this.data.three = true
      }
      this.setData(this.data)
    })

  },


  clear() {
    this.setData({
      keylists: []
    })
    wx.setStorageSync('history', [])
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.showLoading({
      title: '加载中',
    })
    gethotsearch().then(data => {
      console.log(data.data.discover_list);
      this.setData({
        discover_list: data.data.discover_list,
        keylists: wx.getStorageSync('history')
      })
      if (this.data.discover_list) {
        setTimeout(function () {
          wx.hideLoading()
        }, 500)
      }
      // console.log(this.data.discover_list);
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {


  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})